package com.googlecode.nauka.mydbplugin.resulview;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import com.googlecode.nauka.dbfactory.DBFactory;

public class ResultView extends ViewPart{
    public static final String ID = ResultView.class.getName();
    private Table              table;
    private Action             action;
    private Composite          composite;

    public ResultView(){}

    class ExecuteQuery extends Action{
        private Composite parent;
        private String    query;
        private Color     bgcolor;

        public ExecuteQuery(Composite parent){
            this.parent = parent;
        }

        @Override
        public void runWithEvent(Event event){
            query = event.text;
            setPartName(query);
            run();
        }

        @Override
        public void run(){
            if(query == null)
                return;
            if(table != null)
                table.dispose();
            table = new Table(parent, SWT.MULTI | SWT.BORDER
                | SWT.FULL_SELECTION);
            table.setLinesVisible(true);
            table.setHeaderVisible(true);
            table.setItemCount(0);
            bgcolor = new Color(PlatformUI.getWorkbench().getDisplay(), 183,
                252, 150);
            writeData(query);
            table.pack();
        }

        private void writeData(String query){
            try{
                DBFactory db = DBFactory.getDb();
                if(db == null)
                    throw new Exception("No db connection");
                PreparedStatement st = db.prepareStatement(query);
                if(st.execute()){
                    ResultSet rs = st.getResultSet();
                    ResultSetMetaData rsmd = rs.getMetaData();
                    int numColumns = rsmd.getColumnCount();
                    for(int i = 0; i < numColumns; i++){
                        TableColumn column = new TableColumn(table, SWT.NONE);
                        column.setText(rsmd.getColumnName(i + 1));
                    }
                    int k = 0;
                    while(rs.next()){
                        k++;
                        TableItem item = new TableItem(table, SWT.NONE);
                        if(k % 2 == 0)
                            item.setBackground(bgcolor);
                        for(int i = 0; i < numColumns; i++){
                            Object o = rs.getObject(i + 1);
                            if(o != null)
                                item.setText(i, o.toString());
                        }
                    }
                    rs.close();
                    for(int i = 0; i < numColumns; i++){
                        table.getColumn(i).pack();
                    }
                }else{
                    TableColumn column = new TableColumn(table, SWT.NONE);
                    column.setText("Message:");
                    TableItem item = new TableItem(table, SWT.NONE);
                    item.setText("Done");
                    table.getColumn(0).pack();
                }
                st.close();
            }catch(Exception e){
                TableColumn column = new TableColumn(table, SWT.NONE);
                column.setText("Error:");
                TableItem item = new TableItem(table, SWT.NONE);
                item.setText(e.getMessage());
                table.getColumn(0).pack();
            }

        }
    }

    public void createPartControl(Composite parent){
        composite = new Composite(parent, SWT.NORMAL);
        action = new ExecuteQuery(composite);
    }

    public void setFocus(){
        composite.setFocus();
    }

    public Action getAction(){
        return action;
    }
}
